$(function(){
	var data=new jStringBuffer();
	$.getJSON("firstLevel",{},function(firstLevel,status){
		if(firstLevel.length>0){
			$('#header1').show();
			for(var i=0;i<firstLevel.length;i++){
				data.append("<tr><td>").append(firstLevel[i].firCode).append("</td><td>").append(firstLevel[i].firName)
				.append("</td><td id='").append(firstLevel[i].firCode)
				.append("' style='color: blue; cursor:pointer;' onclick='delFirst(this)'>删除</td>");
			}
			
		}
		else{
			data.append("<tr><td rowspan='10' colspan='3'>数据库中无任何分类</td></tr>");
		}
		$('#firstLevel').append(data.toString());
	});
});
$(function(){
	refresh();
})
function querySecondLevel1(){
	$('#secondLevel').html('');
	var data=new jStringBuffer();
	var firCode = document.getElementById("firstSelect2").value;
	if(firCode != "-1"){
		$.getJSON("secondLevel",{firCode:firCode},function(secondLevel,status){
			if(secondLevel.length > 0){
				$('#header2').show();
				var firName = $('#firstSelect2').find("option:selected").text();
				$('#who2').html('');
				$('#who2').append(firName);
				for(var i=0;i<secondLevel.length;i++){
					data.append("<tr><td>").append(secondLevel[i].secCode).append("</td><td align='left'>").append(secondLevel[i].secName)
					.append("</td><td id='").append(secondLevel[i].secCode)
					.append("' style='color: blue; cursor:pointer;' onclick='delSecond(this)'>删除</td>");
				}
			}
			else{
				data.append("<tr><td rowspan='10' colspan='3'>该一级分类下无分类</td></tr>");
			}
			$("#secondLevel").append(data.toString());
		});
	}
}
function querySecondLevel2(){
	$("#secondSelect3").html('');
	$('#thirdLevel').html('');
	$('#header3').hide();
	var firCode = document.getElementById("firstSelect3").value;
	var data=new jStringBuffer();
	if(firCode != "-1"){
		data.append("<option value='-1'>--请选择--</option>");
		$.getJSON("secondLevel",{firCode:firCode},function(secondLevel,status){
			for(var i=0;i<secondLevel.length;i++){
				data.append("<option value='").append(secondLevel[i].secCode)
				.append("'>").append(secondLevel[i].secName).append("</option>");
			}
			$("#secondSelect3").append(data.toString());
		});
	}
}
function queryThirdLevel(){
	$('#thirdLevel').html('');
	var firCode = document.getElementById("firstSelect3").value;
	var secCode = document.getElementById("secondSelect3").value;
	var data=new jStringBuffer();
	if(secCode !="-1"){
		$.getJSON("thirdLevel",{firCode:firCode,secCode:secCode},function(thirdLevel){
			if(thirdLevel.length>0){
				$('#header3').show();
				var firName = $('#firstSelect3').find("option:selected").text();
				var secName = $('#secondSelect3').find("option:selected").text();
				$('#who3').html('');
				$('#who3').append(firName+","+secName);
				for(var i=0;i<thirdLevel.length;i++){
					data.append("<tr><td>").append(thirdLevel[i].thiCode).append("</td><td align='left'>").append(thirdLevel[i].thiName)
					.append("</td><td id='").append(thirdLevel[i].thiCode)
					.append("' style='color: blue; cursor:pointer;' onclick='delThird(this)'>删除</td>");
				}
			}
			else{
				data.append("<tr><td rowspan='10' colspan='3'>该二级分类下无分类</td></tr>");
			}
			$("#thirdLevel").append(data.toString());
		});
	}
}


function refresh(){
	$('#firstSelect2').html('');
	$('#firstSelect3').html('');
	var data=new jStringBuffer();
	$.getJSON("firstLevel",{},function(firstLevel,status){
		data.append("<option value='-1'>--请选择--</option>");
		for(var i=0;i<firstLevel.length;i++){
			data.append("<option value='").append(firstLevel[i].firCode)
			.append("'>").append(firstLevel[i].firName).append("</option>");
		}
		$("#firstSelect2").append(data.toString());
		$("#firstSelect3").append(data.toString());
	});
}

function delFirst(obj){
	lcl.confirm({ message: "将删除该一级分类及其分类下的所有二、三级分类，是否确认删除？" }).on(function (e) {  
       if (!e) {  
           return;  
       }
       var firCode = obj.id;
	   if(firCode != ""){
	   		$.post("delFirst",{firCode:firCode},function(mark){
	   			if(mark == "1"){
	   	    		$(obj).parent().remove();
	   	    		refresh();
	   	    	}
	   	    	else alert("删除失败！");
	   		});
	   	}    
	});
}
function delSecond(obj){
	lcl.confirm({message:"将删除该二级分类及其分类下的所有三级分类，是否确认删除？"}).on(function(e){
		if(!e)return;
		else{
			var firCode = document.getElementById("firstSelect2").value;
		    var secCode = obj.id;
		    if(firCode !="-1"&&secCode != ""){
		    	$.post("delSecond",{firCode:firCode,secCode:secCode},function(mark){
					if(mark == "1"){
			    		$(obj).parent().remove();
			    	}
			    	else alert("删除失败！");
				});
		    }
		}
	});
}
function delThird(obj){
	lcl.confirm({message:"确认删除？"}).on(function(e){
		if(!e)return;
		else{
			var firCode = document.getElementById("firstSelect3").value;
			var secCode = document.getElementById("secondSelect3").value;
			var thiCode = obj.id;
			if(firCode !="-1"&&secCode != "-1"&&thiCode != ""){
				$.post("delThird",{firCode:firCode,secCode:secCode,thiCode:thiCode},function(mark){
					if(mark == "1"){
			    		$(obj).parent().remove();
			    	}
			    	else alert("删除失败！");
				});
			}
		}
	});
	
}